package com.xln.furn.exception;

import com.xln.furn.util.Result;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;

@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(Exception.class)
    public Result ExceptionHandler(Exception e){
        if (e instanceof MethodArgumentNotValidException){
            String message = e.getMessage();
            int start = message.lastIndexOf("default message [") + "default message [".length();
            int end = message.lastIndexOf("]");
            return Result.fail(401,"参数校验异常:");
        }else {
            return Result.fail(501,"服务器未知异常:"+e.getMessage());
        }
    }

}
